package com.ftg.learn.xmlinterface;

import com.ftg.learn.xmlinterface.entity.TeacherEntity;
import com.ftg.learn.xmlinterface.mapper.TeacherMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

/**
 * @author 布衣
 */
public class XmlDemo {
    public static void main(String[] args) {

        try {
            //xml解析  config.xml [数据源、映射器配置文件]
            Reader reader = Resources.getResourceAsReader("config.xml");

            SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
            SqlSessionFactory build = ssfb.build(reader);

            SqlSession sqlSession = build.openSession();

            //动态生成一个代理类  代理类
            TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

            System.out.println(mapper.showTeacherByName("康"));


            sqlSession.commit();
            sqlSession.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
